package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.FilterItem;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/29.
 */

public class UsermanagerGridViewAdapter extends BaseAdapter {
    Context context;
    List<FilterItem> data;
    private Map<Integer, Boolean> isSelected;

    int mSelect = -1;
    public UsermanagerGridViewAdapter(Context context, List<FilterItem> data) {
        this.context = context;
        this.data = data;
        isSelected = new HashMap<Integer, Boolean>();
//        Log.i("--data->","data:"+data.size());
//        Log.i("--data->","data:"+data.toString());
        for (int i = 0; i <data.size(); i++) {
            isSelected.put(i, false);
        }

    }
    public Map<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public void setIsSelected(Map<Integer, Boolean> map) {
        isSelected = map;
        notifyDataSetChanged();
    }
    public void changeSelected(int posion){
        if (posion!=mSelect){
            mSelect = posion;
            notifyDataSetChanged();
        }
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null){
            view = View.inflate(context, R.layout.fragment_gridview_item,null);
            holder = new ViewHolder();
            holder.textView = (TextView) view.findViewById(R.id.fragment_gridview_item_tv);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }

        holder.textView.setText(data.get(i).getItemName().toString());
//        holder.textView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (isSelected.get(i)){
//                    isSelected.put(i,false);
//                    setIsSelected(isSelected);
//                    notifyDataSetChanged();
//                }else {
//                    isSelected.put(i,true);
//                    setIsSelected(isSelected);
//                    notifyDataSetChanged();
//                }
//            }
//        });
        if (isSelected.get(i))
        {
            holder.textView.setBackgroundResource(R.drawable.fragment_gridview_item_back2);
            holder.textView.setTextColor(context.getResources().getColor(R.color.light_blue));
        }else {
            holder.textView.setTextColor(context.getResources().getColor(R.color.colorGridViewTextViews));
            holder.textView.setBackgroundResource(R.drawable.fragment_gridview_item_back1);
        }
//        if (mSelect == i){
//            holder.textView.setBackgroundResource(R.drawable.fragment_gridview_item_back2);
//            holder.textView.setTextColor(context.getResources().getColor(R.color.light_blue));
//        }else {
//            holder.textView.setTextColor(context.getResources().getColor(R.color.colorGridViewTextViews));
//            holder.textView.setBackgroundResource(R.drawable.fragment_gridview_item_back1);
//        }
        return view;
    }
    static class ViewHolder{
        TextView textView = null;
    }
}
